<?php

/**
 *  This is the main controller of the Application. If the user enters
 *  the domain of the site, this is the controller they are routed to.
 **/
//if(!class_exists('MAIN_Controller_Flow_Action')) Zend_Loader :: loadClass('MAIN_Controller_Flow_Action');

class ExampleController extends MAIN_Controller_Action {

	public function reSizeImage($images,$new_filename){
		if(!$images)return;
		$new_file = UPLOAD_PATH."/member/".$new_filename;
		$height=80;
		$size=GetimageSize($images);
		$width=round($height*$size[0]/$size[1]);
		if($size['mime'] == 'image/jpeg')
			$images_orig = ImageCreateFromJPEG($images);
		elseif($size['mime'] == 'image/gif')
			$images_orig = ImageCreateFromGIF($images);
		elseif($size['mime'] == 'image/png')
			$images_orig = ImageCreateFromPNG($images);
		else
			return;
		$photoX = ImagesX($images_orig);
		$photoY = ImagesY($images_orig);
		$images_fin = ImageCreateTrueColor($width, $height);
		#_print($images_fin);
		ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		if($size['mime'] == 'image/jpeg')
			ImageJPEG($images_fin,$new_file.".jpg"); // ชื่อไฟล์ใหม่
		elseif($size['mime'] == 'image/gif')
			ImageGIF($images_fin,$new_file.".gif"); // ชื่อไฟล์ใหม่
		elseif($size['mime'] == 'image/png')
			ImageGIF($images_fin,$new_file.".png"); // ชื่อไฟล์ใหม่
		ImageDestroy($images_fin);
		#unlink($images);
	}

	public function reSizePic($images){
		if(!$images)return;
		//กำหนดความสูงของรูปใหม่
		$height=400;
		$size=GetimageSize($images);
		$width=round($height*$size[0]/$size[1]);
		$images_orig = ImageCreateFromJPEG($images);
		$photoX = ImagesX($images_orig);
		$photoY = ImagesY($images_orig);
		$images_fin = ImageCreateTrueColor($width, $height);
		ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
		ImageJPEG($images_fin,UPLOAD_PATH."/member/newimages.jpg");
		ImageDestroy($images_orig);
		ImageDestroy($images_fin);
	}

    public function displayAction() {
        /***
         * $view = $this->_getView();
         * $view->output();
         * $this->_forward('index', 'frontend', 'frontend', $this->getParams());
         */

        //create new GD renderer, optinal parameters: LevelSeparation,  SiblingSeparation, SubtreeSeparation, defaultNodeWidth, defaultNodeHeight
        Zend_Loader :: loadClass("MAIN_TreeChart");
        #$objTree= new MAIN_TreeChart(30, 50, 30, 50, 20);
        $objTree= new MAIN_TreeChart(30, 10, 10, 50, 20);

        //add nodes to the tree, parameters: id, parentid optional text, width, height, image(path)
        //$data=array("a","b","c","d","e","f");

        $data[10]["title"]= "Mr. Test Tester\nP. Consountance";
        $data[10]["parent"]= 6;
        $data[10]["width"]= 200;
        $data[10]["height"]= 40;
        $data[10]["href"]= "#";
        $data[10]["function"]= array (
            "onmousedown" => "",
            "onmousemove" => "",
            "onmouseout" => "",
            "onmouseover" => "",
            "onclick" => "alert(this.title)"
        );

        $data = $this->renderData();

        //$data[12]["img"]="";//croc.png
        /*$data["13"]["title"]="h1";
        $data["13"]["parent"]="12";*/
        $i= 0;
        foreach ($data as $_key => $_item) {
            $objTree->add($_key, $_item["parent"], $_item["title"], $_item["width"], $_item["height"], $_item["img"], $_item["href"], $_item["alt"], $_item["function"]);
        }

        $objTree->setBGColor(array (
            255,
            255,
            255
        ));
        $objTree->setNodeColor(array (
            215,
            237,
            248
        ));
        $objTree->setLinkColor(array (
            0,
            64,
            128
        ));
        #$objTree->setNodeLinks(GDRenderer::LINK_BEZIER);
        $objTree->setNodeBorder(array (
            0,
            128,
            255
        ), 1);
        $objTree->setFTFont(PROJECT_LIBRARY_PATH."fonts/Vera.ttf", 8, 0, MAIN_TreeChart :: CENTER | MAIN_TreeChart :: BOTTOM);

        // $objTree->stream();
        $file= "test.png";
        $filepath = UPLOAD_PATH."/".$file;
        $fileurl = UPLOAD_URL."$file";

        $objTree->save($filepath);
        echo "<img id=\"organize-chart\" src=\"$fileurl\" ismap usemap=\"#myimagemap\" border=0/>";
        echo $objTree->GetHTMLImageMap("myimagemap");
        //
    }

    public function renderNewData(&$data,$dataArr){
		if(!$dataArr)return;
		foreach($dataArr as $key=>$item){
			if($data[$item['parent_id']] == $item['chart_id']){
				$data[$item['parent_id']] = $key;
			}
		}
    }

    public function renderData(){
		$generic = System_Controller::getGeneric('Org');
		$OrgArr=$generic->getOrgani();
		#_print($OrgArr);
		$i=0;
		foreach($OrgArr as $item){
			$i++;
			$this->renderNewData(&$item,$dataArr);
			$dataArr[$i]["chart_id"]= $item['chart_id'];
			$dataArr[$i]["title"]= "p_id --> ".$item['parent_id']." id => ".$i."[".$item['parent_lv'].":".$item['chart_level']."]";//$item['chart_name']
			$dataArr[$i]["level"]= $item['chart_level'];
			$dataArr[$i]["parent_lv"]= $item['parent_lv'];
			$dataArr[$i]["parent"]= $item['parent_id'];
			$dataArr[$i]["img"]= $item['chart_image'];
			$_img=($item['chart_image'])?UPLOAD_PATH."/member/".$item['chart_image']:UPLOAD_PATH."/member/no_pic.gif";
			#$this->reSizeImage($_img,"org_".$item['chart_id']);
			$imgSize=getimagesize($_img);
			$maxHeight=($maxHeight > $imgSize['0'])?$maxHeight:$imgSize['0'];
			$maxwidth=($maxwidth > strlen($item['chart_name']))?$maxwidth:strlen($item['chart_name']);
		}
		/*#_print($dataArr);
		$i=1;
		$data[$i]["title"]= "CEO  =>".$i;
		$data[$i]["level"]= 1;
		$data[$i]["parent"]= 0;
		$i++;
		$data[$i]["title"]= "2.1=>".$i;
		$data[$i]["level"]= 2;
		$data[$i]["parent_lv"]= 1;
		$data[$i]["parent"]= 1;
		$i=3;
		$data[$i]["title"]= "2.2=>".$i;
		$data[$i]["level"]= 2;
		$data[$i]["parent_lv"]= 1;
		$data[$i]["parent"]= 1;
		$i=4;
		$data[$i]["title"]= " 2.3=>".$i;
		$data[$i]["level"]= 2;
		$data[$i]["parent_lv"]= 1;
		$data[$i]["parent"]= 1;
		$i=5;
		$data[$i]["title"]= " 3.1=>".$i;
		$data[$i]["level"]= 3;
		$data[$i]["parent_lv"]= 2;
		$data[$i]["parent"]= 2;
		$i=6;
		$data[$i]["title"]= " 3.2=>".$i;
		$data[$i]["level"]= 3;
		$data[$i]["parent_lv"]= 2;
		$data[$i]["parent"]= 2;
		$i=7;
		$data[$i]["title"]= " 3.3=>".$i;
		$data[$i]["level"]= 3;
		$data[$i]["parent_lv"]= 2;
		$data[$i]["parent"]= 3;
		$i=8;
		$data[$i]["title"]= " 4.1=>".$i;
		$data[$i]["level"]= 4;
		$data[$i]["parent_lv"]= 1;
		$data[$i]["parent"]= 1;

		$i=9;
		$data[$i]["title"]= "4.2=>".$i;
		$data[$i]["level"]= 4;
		$data[$i]["parent_lv"]= 2;
		$data[$i]["parent"]= 2;

		$i=10;
		$data[$i]["title"]= "4.3=>".$i;
		$data[$i]["level"]= 4;
		$data[$i]["parent_lv"]= 2;
		$data[$i]["parent"]= 4;

		$i=11;
		$data[$i]["title"]= "5.1=>".$i;
		$data[$i]["level"]= 5;
		$data[$i]["parent_lv"]= 4;
		$data[$i]["parent"]= 10;*/

    	$i=0;
    	if($dataArr)
    		foreach($dataArr as $key=>$item){
				$i++;
				#echo "<br> ===>".$i;
				$title =$item['title'];
				$level =$item['level'];
				$img=($item['img'])?UPLOAD_PATH."/member/".$item['img']:UPLOAD_PATH."/member/no_pic.gif";
				$title="header=[".$item[title]."] body=[<table width='100%'>{$img}<tr><td align='center'>".$item[level]."&nbsp;".$item[parent_lv]."</td></tr><tr><td>Tel. ".$item[title]."&nbsp;ext.&nbsp;".$item[title]."</td></tr><tr><td>Email : ".$item[title]."</td></tr></table>]";
				$width=$maxwidth+100;
				$height=($maxHeight)?$maxHeight+50:40;
				$function = $item['function'];
				$_oldid[$key] = $i;
				if(($item['parent_lv']+1) < $level){
					for($j=$item['parent_lv']+1; $j <= $level;$j++){
							$item['title']=""; $item["width"]=""; $item["height"]="";$item["img"]="";
							if($j >1){
								if($j == $level){
									$item['title']=$title;
									$item["width"]= $width;
	        						$item["height"]= $height;
	        						$item["img"]=$img;
	        						$item["function"]= $function;
	        						$item['parent']=$check[$item[level]][$item[parent_lv]];
								}else{
									if($_last[level] == $item[level] && $_last[parent_lv] == $item[parent_lv] && $_last[parent]==$item[parent])
										$item['parent']=$_last[i];
									else
										$item['parent']=$_oldid[$item[parent]];
									$check[$item[level]][$item[parent_lv]] =$i;
								}
								$item[i] = $i;
								$_last=$item;
							}
							$newData[$i]=$item;
							$_oldid[$key] = $i;
							if($j < $level) $i++;
					}
				}else{
					$item['id']=$i;
					$item["width"]= $width;
        			$item["height"]= $height;
        			$item["img"]=$img;
        			$item["parent"]=$_oldid[$item[parent]];
					$newData[$i]=$item;
				}

    		}
//    	_print($_oldid);
//    	_print($newData);
    	#$newData =$data;
    	return $newData;
    }
}